<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/22
 * Time: 15:30
 */

namespace App\Api\Admin\Controllers;

use App\Exceptions\ApiException;
use App\Models\Branch;
use App\Models\Admin\Log;
use Illuminate\Http\Request;

class BranchController extends CommonController
{
    public function __construct()
    {
        parent::__construct();

    }
    /**
     * @param Request $request
     * @return array
     * 分公司列表
     */
    public function index(Request $request)
    {
        $pagesize = (int) $request->input('pageszie');
        $pagesize = $pagesize ? : 10;
        $data = Branch::where('status',0)->select('id','locality_name','created_at')->paginate($pagesize);
        $temp_arr = [];
        $list_data = [];
        foreach ($data as $list){
            $temp_arr['id'] = $list->id;
            $temp_arr['name'] = $list->locality_name;
            $temp_arr['created_at'] = $list->created_at;
            $list_data[] = $temp_arr;
        }
        return json_success('ok',['total'=>$data->total(),'totalPage' => $data->lastPage(),'list' => $list_data ] );
    }

    /**
     * @return array
     * 添加分公司
     */
    public function AddBranch(){
        $name = IT('name',true,'请输入城市名称');
        $check_City = Branch::where(['locality_name'=>$name,'status'=>0])->first();
        if($check_City){
         return json_error('分公司已存在');
        }
        $data=[
            'locality_name' =>$name,
            'created_at' => time2date(),
        ];
         Branch::insert($data);
        (new Log)->add($this->user,'添加了分公司'.'名称为-'.$name);
        return json_success('ok');
        return json_error('添加失败');
    }

    /**
     *分公司删除 修改
     */
    public function DelBranch(){
        $id = IT('id',true,'请输入城市id');
        $behavior= IT('behavior',true,'请输入你要操作的行为');
        $check_City = Branch::where(['id'=>$id,'status'=>0])->first();
        if(!$check_City){
            return json_error('分公司不存在');
        }
        if($behavior =='del'){
            $check_City->update(['status'=>1]);
            (new Log)->add($this->user,'删除了分公司'.'名称为-'.$check_City->locality_name);
        }
        if($behavior =='update'){
            $name = IT('name',true,'请输入你要修改的名称');
            $check_name = Branch::where(['locality_name'=>$name,'status'=>0])->first();
            if($check_name){
                return json_error('分公司名称已存在');
            }
            $check_City->update(['locality_name'=>$name]);
            (new Log)->add($this->user,'删除了分公司'.'名称为-'.$name);
        }
        return json_success('ok');
        return json_error('删除失败');
    }

    /**
     * @return array
     * 分公司选择列表
     */
    public  function ChoiceBranch(){
        $data = Branch::where('status',0)->select('id','locality_name','created_at')->get();
        $temp_arr = [];
        $list_data = [];
        foreach ($data as $list){
            $temp_arr['id'] = $list->id;
            $temp_arr['name'] = $list->locality_name;
            $temp_arr['created_at'] = $list->created_at;
            $list_data[] = $temp_arr;
        }
        return json_success('ok',$list_data );
    }
    /***
     *用户归属地
     *
     */
    public function UserAttribution(){
        $city_of_origin = $this->user['city_of_origin'];
        $Attribution= D("affiliation")->where('id',$city_of_origin)->get(['id','locality_name']);
        return json_success('ok',$Attribution);
    }





}